"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : GUC
Case Name   : instr_unique_sql_track_type
Create At   : 2021/1/25
Owner       : opentestcase018
Description :
            1、修改instr_unique_sql_track_type为all，校验其预期结果；
            gs_guc set -D {cluster/dn1} -c "instr_unique_sql_track_type=all";
            2、恢复默认值；
Expect      :
            1、参数修改成功；
            2、恢复默认值；
History     :
            Modified by opentestcase013 at 2023/10/25:instr_unique_sql_track_type
            现可支持修改，遂将此用例优化为成功修改参数值为all
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Guctestcase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        status = self.pri_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        self.default_param = self.common.show_param(
            'instr_unique_sql_track_type')
        self.log.info(self.default_param)

    def test_guc(self):
        text = '-----step1:修改instr_unique_sql_track_type为all，' \
               '校验其预期结果;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execute_gsguc('set',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        'instr_unique_sql_track_type=all')
        self.log.info(res)
        self.assertTrue(res)
        restart_res = self.pri_sh.restart_db_cluster()
        self.log.info(restart_res)
        self.assertTrue(restart_res, "执行失败" + text)

    def tearDown(self):
        text = '-----step2:恢复默认值;expect:成功-----'
        self.log.info(text)
        curr_param = self.common.show_param('instr_unique_sql_track_type')
        self.log.info(curr_param)
        if curr_param != self.default_param:
            res = self.pri_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'instr_unique_sql_track_type={self.default_param}')
            self.log.info(res)
            self.assertTrue(res)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')